package com.Alex.ComicOnline;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DbAdapter {
	public static final String KEY_BOOK_ID = "_bookid";
	public static final String KEY_BOOK_NAME = "name";
	public static final String KEY_BOOK_LINK = "link";
	public static final String KEY_BOOK_START = "start";
	public static final String KEY_BOOK_END = "end";
	public static final String KEY_BOOK_ELEMENTID = "elementid";

	private static final String DATABASE_NAME = "comic";
	private static final String DATABASE_TABLE_BOOK = "books";
	private static final int DATABASE_VERSION = 1;

	private static final String DATABASE_CREATE = "create table books (_bookid integer primary key autoincrement, "
			+ "name text not null, link text not null, start integer not null, "
			+ "end integer not null, elementid text not null);";

	private static final String TAG = "DbAdapter";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS notes");
			onCreate(db);
		}
	}

	public DbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public DbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public long createBook(String name, String link, int start, int end,
			String elementid) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_BOOK_NAME, name);
		initialValues.put(KEY_BOOK_LINK, link);
		initialValues.put(KEY_BOOK_START, start);
		initialValues.put(KEY_BOOK_END, end);
		initialValues.put(KEY_BOOK_ELEMENTID, elementid);

		return mDb.insert(DATABASE_TABLE_BOOK, null, initialValues);
	}

	public boolean updateNote(String name, String link, int start, int end,
			String elementid, long id) {
		ContentValues args = new ContentValues();
		args.put(KEY_BOOK_NAME, name);
		args.put(KEY_BOOK_LINK, link);
		args.put(KEY_BOOK_START, start);
		args.put(KEY_BOOK_END, end);
		args.put(KEY_BOOK_ELEMENTID, elementid);

		return mDb.update(DATABASE_TABLE_BOOK, args, KEY_BOOK_ID + "=" + id,
				null) > 0;
	}

	public boolean deleteBook(long id) {
		return mDb.delete(DATABASE_TABLE_BOOK, KEY_BOOK_ID + "=" + id, null) > 0;
	}

	public Cursor fetchAllBooks() {

		return mDb.query(DATABASE_TABLE_BOOK, new String[] { KEY_BOOK_ID,
				KEY_BOOK_NAME, KEY_BOOK_LINK, KEY_BOOK_START, KEY_BOOK_END,
				KEY_BOOK_ELEMENTID }, null, null, null, null, null);
	}

	public Cursor fetchNote(long id) throws SQLException {
		Cursor mCursor = mDb.query(true, DATABASE_TABLE_BOOK, new String[] {
				KEY_BOOK_ID, KEY_BOOK_NAME, KEY_BOOK_LINK, KEY_BOOK_START,
				KEY_BOOK_END, KEY_BOOK_ELEMENTID }, KEY_BOOK_ID + "=" + id,
				null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
}
